package com.boby.homecamera.db

import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.os.Parcelable
import androidx.core.database.getStringOrNull
import kotlinx.parcelize.Parcelize

class CameraDeviceTable(val database:SQLiteDatabase) {


     companion object {
         private   val TABLE_USER = "cameradevice"
         fun createTable(db: SQLiteDatabase) {

             db.execSQL(
                 """
            CREATE TABLE IF NOT EXISTS $TABLE_USER (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                userName TEXT NOT NULL,
                password TEXT NOT NULL,
                token TEXT ,
                refreshToken TEXT ,
                deviceId TEXT NOT NULL,
                deviceName TEXT NOT NULL,
                ip TEXT NOT NULL,
                sort INTEGER NOT NULL,
                httpPort INTEGER NOT NULL,
                streamPort INTEGER NOT NULL,
                websocketPort INTEGER NOT NULL
            );
            """.trimIndent()
             )
         }
     }


    fun insert(cameraDevice: CameraDevice): Long {
        val values = ContentValues().apply {
            put("userName", cameraDevice.userName)
            put("password", cameraDevice.password)
            put("token", cameraDevice.token)
            put("refreshToken", cameraDevice.refreshToken)
            put("deviceId", cameraDevice.deviceId)
            put("deviceName", cameraDevice.deviceName)
            put("ip", cameraDevice.ip)
            put("sort", cameraDevice.sort)
            put("httpPort", cameraDevice.httpPort)
            put("streamPort", cameraDevice.streamPort)
            put("websocketPort", cameraDevice.websocketPort)
        }
        return database.insert(TABLE_USER, null, values)
    }
    fun delete(id:Long): Int {
        return  database.delete(TABLE_USER,"id = ?",arrayOf(id.toString()))
    }


    fun update(cameraDevice: CameraDevice): Int {
        val values = ContentValues().apply {
            put("userName", cameraDevice.userName)
            put("password", cameraDevice.password)
            put("token", cameraDevice.token)
            put("refreshToken", cameraDevice.refreshToken)
            put("deviceId", cameraDevice.deviceId)
            put("deviceName", cameraDevice.deviceName)
            put("ip", cameraDevice.ip)
            put("sort", cameraDevice.sort)
            put("httpPort", cameraDevice.httpPort)
            put("streamPort", cameraDevice.streamPort)
            put("websocketPort", cameraDevice.websocketPort)
        }
        return database.update(TABLE_USER, values, "id=?", arrayOf(cameraDevice.id.toString()))
    }


    fun queryAll(): MutableList<CameraDevice> {
        val videos = mutableListOf<CameraDevice>()
        val cursor: Cursor = database.query(
            TABLE_USER,
            arrayOf("id", "userName", "password", "token", "refreshToken",
                "deviceId","deviceName","ip","sort","httpPort","streamPort","websocketPort"),
            null, null, null, null, null
        )
        cursor.use {
            while (it.moveToNext()) {
                val user = CameraDevice(
                    id = it.getLong(it.getColumnIndexOrThrow("id")),
                    userName = it.getString(it.getColumnIndexOrThrow("userName")),
                    password = it.getString(it.getColumnIndexOrThrow("password")),
                    token = it.getStringOrNull(it.getColumnIndexOrThrow("token")),
                    refreshToken = it.getStringOrNull(it.getColumnIndexOrThrow("refreshToken")),
                    deviceId = it.getString(it.getColumnIndexOrThrow("deviceId")),
                    deviceName = it.getString(it.getColumnIndexOrThrow("deviceName")),
                    ip = it.getString(it.getColumnIndexOrThrow("ip")),
                    sort = it.getInt(it.getColumnIndexOrThrow("sort")),
                    httpPort = it.getInt(it.getColumnIndexOrThrow("httpPort")),
                    streamPort = it.getInt(it.getColumnIndexOrThrow("streamPort")),
                    websocketPort = it.getInt(it.getColumnIndexOrThrow("websocketPort"))
                )
                videos.add(user)
            }
        }
        return videos
    }

    fun queryByDeviceId(deviceId:String): CameraDevice? {
        val cursor: Cursor = database.query(
            TABLE_USER,
            arrayOf("id", "userName", "password", "token", "refreshToken","deviceId","deviceName","ip","sort","httpPort","streamPort","websocketPort"),
            "id = ?", arrayOf(deviceId), null, null, null
        )
        cursor.use {
            while (it.moveToFirst()) {
                return CameraDevice(
                    id = it.getLong(it.getColumnIndexOrThrow("id")),
                    userName = it.getString(it.getColumnIndexOrThrow("userName")),
                    password = it.getString(it.getColumnIndexOrThrow("password")),
                    token = it.getStringOrNull(it.getColumnIndexOrThrow("token")),
                    refreshToken = it.getStringOrNull(it.getColumnIndexOrThrow("refreshToken")),
                    deviceId = it.getString(it.getColumnIndexOrThrow("deviceId")),
                    deviceName = it.getString(it.getColumnIndexOrThrow("deviceName")),
                    ip = it.getString(it.getColumnIndexOrThrow("ip")),
                    sort = it.getInt(it.getColumnIndexOrThrow("sort")),
                    httpPort = it.getInt(it.getColumnIndexOrThrow("httpPort")),
                    streamPort = it.getInt(it.getColumnIndexOrThrow("streamPort")),
                    websocketPort = it.getInt(it.getColumnIndexOrThrow("websocketPort"))
                )
            }
        }
        return null
    }

    @Parcelize
    data class CameraDevice(
        var id: Long = -1,
        var userName: String = "",
        var password: String =  "",
        var token: String? = null,
        var refreshToken: String? = null,
        var deviceId:String = "",//设备id
        var deviceName:String = "",
        var ip:String = "",
        var sort:Int = 0,
        var httpPort:Int = 9200,
        var streamPort:Int = 9201,
        var websocketPort:Int = 9202,
        var notify: Boolean = false,
    ):Parcelable
}


